#!/bin/bash

# 查找空间占用大的容器

# 不同的容器管理命令
alias container_cmd=docker
# alias container_cmd=podman

# 开启alias扩展，否则找不到命令：container_cmd
shopt -s expand_aliases

container_dir="/var/lib/containers/storage/overlay/"
cd "$container_dir"

if [[ $# -eq 1 ]];
then
  cd $1
fi

for dir_i in $(du -sh *|sort -hr |head -n 10 |awk '{print $2}');
do
  for container_i in $(container_cmd ps -a |awk '{if (NR>1){print $1}}');
  do
    if [[ ! $(container_cmd inspect $container_i |grep $dir_i |wc -l) -eq 0 ]];      
      then
        target_dir="$container_dir/$dir_i";
        dir_used=$(du -sh "$target_dir");
        container_name=$(container_cmd inspect --format='{{.Name}}' $container_i);
        echo "$container_name ==> $dir_used";
    fi
  done;
done;
